package com.jzhao.jpre.comm.test;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.InterruptedException;
import java.lang.StringBuffer;
import java.math.BigDecimal;
import java.net.Socket;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import com.jzhao.jpre.log.GeneralLog;
import com.jzhao.jpre.security.ByteShow;

class DevThread implements Runnable {
	Thread t = null;
	Socket skt = null;
	InputStream is = null;
	OutputStream os = null;
	Timestamp CurrTime = null;
	SimpleDateFormat sdf1 = null;
	SimpleDateFormat sdf2 = null;
	int seq = 0;
	int devId = 0;

	DevThread(int seq, int devId) {
		this.seq = seq + 1;
		this.devId = devId + 1;
		t = new Thread(this);
		t.start();
	}

	public void run() {
		try {
			CurrTime = new Timestamp(System.currentTimeMillis());
			sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
			sdf2 = new SimpleDateFormat("yyyyMMdd");

			while (true) {
				byte[] b = new byte[256];
				System.out
						.println("--------Please input test type: s(serial) counts or p(parallel) counts messagefilepath, q to exit");
				int k;
				try {
					k = System.in.read(b);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					continue;

				}
				if (k == -1)
					break;
				if (b[0] == 'q' || b[0] == 'Q')
					break;
				String trancode2 = new String(b,"ISO-8859-1");
				String trancode = trancode2.substring(0, 9);
				String devNo = new String();
				if (trancode.equals("028900001"))
					devNo = "009002";
				else
					devNo = "009002";
				String devSeqNo = "00000000";
				
				StringBuffer sendBuff = new StringBuffer(trancode);
				sendBuff.append("|");
				sendBuff.append(sdf2.format(CurrTime));
				sendBuff.append("|000021|");
				sendBuff.append(devNo);
				sendBuff.append("|");
				sendBuff.append(devSeqNo);
				sendBuff.append("|00000000|");
				
				if (trancode.equals("028900001")){
					
				}
				if (trancode.equals("028900002")) {
					sendBuff.append("02800001|9003011600000702|");
				}
				if (trancode.equals("028900003")) {
					sendBuff.append("20080109|00014825|");
				}
				if (trancode.equals("028001001")){
					sendBuff.append("0010|83835200|");
				}
				if (trancode.equals("028003001")){
					sendBuff.append("8019|");
				}
				if (trancode.equals("028003002")){
					sendBuff.append("02800001|9003011600000702|1748.00|||DE859C249BED80DB||12345||8019|1|20060201|1748.00|");
				}
				if (trancode.equals("028003003")){
					
				}
				if (trancode.equals("028005001")){
					sendBuff.append("028|77008001|");
				}
				if (trancode.equals("028007001")){
					sendBuff.append("02800001|9003011600000702|||DE859C249BED80DB|BEDCF44803E3F438");
				}
					
				if (trancode.equals("128007001")){
					sendBuff.append("02800001|9003011600000702|||DE859C249BED80DB|1111111111111111");
				}
				if (trancode.equals("028007002")){
					sendBuff.append("02800001|9003011600000702|||DE859C249BED80DB||12345|");
				}
				if (trancode.equals("128007002")){
					sendBuff.append("02800001|9003011600000702|||DE859C249BED80DB||12345|1111111111111111");
				}
				if (trancode.equals("100002000")){
					byte[] out3 = new byte[]{0x30,0x32,0x37,0x34,0x31,0x30,0x30,0x30,0x30,0x32,0x30,0x32,0x38,0x30,0x30,0x33,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x30,0x30,0x30,0x31,0x32,0x32,0x00,0x00,0x00,0x00,0x30,0x32,0x30,0x30,0x37,0x31,0x31,0x31,0x33,0x32,0x30,0x30,0x37,0x31,0x31,0x31,0x33,0x30,0x31,0x30,0x31,0x30,0x31,0x41,0x30,0x30,0x31,0x41,0x30,0x30,0x30,0x30,0x31,0x00,0x00,0x30,0x30,0x30,0x30,0x30,0x31,0x32,0x32,0x00,0x00,0x00,0x00,0x37,0x32,0x35,0x30,0x30,0x39,0x30,0x30,0x31,0x00,0x00,0x30,0x30,0x30,0x30,0x31,0x32,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
					skt = new Socket("192.168.1.119", 9001);
					os = skt.getOutputStream();
					is = skt.getInputStream();
					os.write(out3);
					CurrTime = new Timestamp(System.currentTimeMillis());
					byte len[] = new byte[4];
					is.read(len, 0, len.length);
					CurrTime = new Timestamp(System.currentTimeMillis());
					String sLen = new String(len);
					int iLen = Integer.parseInt(sLen);
					byte buff[] = new byte[iLen];
					is.read(buff, 0, buff.length);
					
				    GeneralLog.warn("DevSimu", "buff=" + ByteShow.show(buff) );
				    GeneralLog.warn("DevSimu", "len=" + buff.length );
					System.out.println(sdf1.format(CurrTime) + " Recv["
							+ new String(len) + new String(buff) + "]");
					is.close();
					os.close();
					skt.close(); 
					continue;
				}
				if(trancode.equals("028004001")){
					sendBuff.append("00518982|");
				}
			    if(trancode.equals("028004002")){
			    	sendBuff.append("02800001|9003011600000702|48.00|||DE859C249BED80DB||12345||00518982|");
			    }
			    if(trancode.equals("028004003")){
			    	sendBuff.append("00518982|");
			    }
				DecimalFormat df = new DecimalFormat("0000");
				String sendbuf = df.format( sendBuff.toString().length()) + sendBuff.toString();
				
				
				skt = new Socket("192.168.1.119", 9000);
				os = skt.getOutputStream();
				is = skt.getInputStream();
				os.write(sendbuf.getBytes());
				CurrTime = new Timestamp(System.currentTimeMillis());
				System.out.println(sdf1.format(CurrTime) + " Send["
						+ sendbuf + "]");
				byte len[] = new byte[4];
				is.read(len, 0, len.length);
				CurrTime = new Timestamp(System.currentTimeMillis());
				String sLen = new String(len);
				System.out.println(len);
				System.out.println(sLen.toString());
				int iLen = Integer.parseInt(sLen);
				byte buff[] = new byte[iLen];
				is.read(buff, 0, buff.length);
				
			    GeneralLog.warn("DevSimu", "buff=" + ByteShow.show(buff) );
			    GeneralLog.warn("DevSimu", "len=" + buff.length );
				System.out.println(sdf1.format(CurrTime) + " Recv["
						+ new String(len) + new String(buff) + "]");
				is.close();
				os.close();
				skt.close();
			}

		} catch (IOException ioe) {
			System.out.println(ioe);
			}

	}

	/*
	 * 
	 * public void run() { try { CurrTime = new
	 * Timestamp(System.currentTimeMillis()); sdf1 = new
	 * SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); sdf2 = new
	 * SimpleDateFormat("yyyyMMdd"); String devNo = "009002"; String devSeqNo =
	 * "00000000";
	 * 
	 * 
	 * byte[]{0x30,0x32,0x37,0x34,0x31,0x30,0x30,0x30,0x30,0x32,0x30,0x32,0x38,0x30,0x30,0x33,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x30,0x30,0x30,0x31,0x32,0x32,0x00,0x00,0x00,0x00,0x30,0x32,0x30,0x30,0x37,0x31,0x31,0x31,0x33,0x32,0x30,0x30,0x37,0x31,0x31,0x31,0x33,0x30,0x31,0x30,0x31,0x30,0x31,0x41,0x30,0x30,0x31,0x41,0x30,0x30,0x30,0x30,0x31,0x00,0x00,0x30,0x30,0x30,0x30,0x30,0x31,0x32,0x32,0x00,0x00,0x00,0x00,0x37,0x32,0x35,0x30,0x30,0x39,0x30,0x30,0x31,0x00,0x00,0x30,0x30,0x30,0x30,0x31,0x32,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
	 * 
	 * StringBuffer sendBuff = new StringBuffer("0043028900001|");
	 * sendBuff.append(sdf2.format(CurrTime)); sendBuff.append("|000021|");
	 * sendBuff.append(devNo); sendBuff.append("|"); sendBuff.append(devSeqNo);
	 * sendBuff.append("||"); skt = new Socket("127.0.0.1", 9001); os =
	 * skt.getOutputStream(); is = skt.getInputStream(); os.write(out3);
	 * System.out.println(sdf1.format(CurrTime) + " Send[" + out3.toString() +
	 * "]"); byte len[] = new byte[4]; is.read(len, 0, len.length); CurrTime =
	 * new Timestamp(System.currentTimeMillis()); String sLen = new String(len);
	 * int iLen = Integer.parseInt(sLen); byte buff[] = new byte[iLen];
	 * is.read(buff, 0, buff.length); System.out.println(sdf1.format(CurrTime) + "
	 * Recv[" + new String(len) + new String(buff) + "]"); is.close();
	 * os.close(); skt.close(); } catch (IOException ioe) {
	 * System.out.println(ioe); } }
	 * 
	 */
}

public class DevSimu {
	public static void main(String args[]) {
		for (int cycle = 0; cycle < 1; cycle++) {
			for (int concurrent = 0; concurrent < 1; concurrent++) {
				new DevThread(cycle, concurrent);
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException ie) {
				System.out.println(ie);
			}
		}
	}
}